package com.xin.ruanjian.omsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.adapter.ShopCarAdapter;
import com.xin.ruanjian.omsystem.adapter.ShopDetailVpAdapter;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bases.BaseActivity;
import com.xin.ruanjian.omsystem.bases.Login;
import com.xin.ruanjian.omsystem.bean.IndexBean;
import com.xin.ruanjian.omsystem.bean.MessageEvent;
import com.xin.ruanjian.omsystem.model.ShopCarModel;
import com.xin.ruanjian.omsystem.utils.DoubleUtils;
import com.xin.ruanjian.omsystem.utils.ShopCarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopDetailActivity extends BaseActivity {
    @BindView(R.id.tv_centerTitle)
    TextView tvCenterTitle;
    @BindView(R.id.iv_rightPic)
    ImageView ivRightPic;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_shop_banner)
    ImageView ivShopBanner;
    @BindView(R.id.ct_shop_detail)
    CollapsingToolbarLayout ctShopDetail;
    @BindView(R.id.tl_shop_detail)
    TabLayout tlShopDetail;
    @BindView(R.id.al_shop_detail)
    AppBarLayout alShopDetail;
    @BindView(R.id.vp_shop_detail)
    ViewPager vpShopDetail;
    @BindView(R.id.tv_shop_cart_all_count)
    TextView tvShopCartAllCount;
    @BindView(R.id.tv_shop_cart_fee)
    TextView tvShopCartFee;
    @BindView(R.id.tv_shop_cart_checkout)
    TextView tvShopCartCheckout;
    @BindView(R.id.ll_shop_car_info)
    LinearLayout llShopCarInfo;
    @BindView(R.id.tv_shop_car_window_clear)
    TextView tvShopCarWindowClear;
    @BindView(R.id.rv_shop_car_window_list)
    RecyclerView rvShopCarWindowList;
    @BindView(R.id.ll_shop_car_list)
    LinearLayout llShopCarList;
    @BindView(R.id.iv_shop_car_icon)
    ImageView ivShopCarIcon;
    @BindView(R.id.rl_shop_car_icon)
    public RelativeLayout rlShopCarIcon;
    @BindView(R.id.tv_count)
    TextView tvCount;
    private ShopDetailVpAdapter adapter;
    private ShopCarAdapter shopCarAdapter;
    private IndexBean.MsgBean.ShopBean shopBean;
    private Login login = MyApplication.getLogin();

    @Override
    protected void initContentView(Bundle bundle) {
        setContentView(R.layout.activity_shop_detail);
    }

    @Override
    protected void initData() {
        shopBean = (IndexBean.MsgBean.ShopBean) getIntent().getSerializableExtra("shop_info");
    }

    @Override
    protected void initView() {
        adapter = new ShopDetailVpAdapter(context, getSupportFragmentManager(),
                Arrays.asList("商品", "评价", "详情"), shopBean);
        vpShopDetail.setOffscreenPageLimit(3);
        vpShopDetail.setAdapter(adapter);
        tlShopDetail.setupWithViewPager(vpShopDetail);

        rvShopCarWindowList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvShopCarWindowList.setItemAnimator(null);
        shopCarAdapter = new ShopCarAdapter(context);
        shopCarAdapter.setListener(new ShopCarAdapter.onItemClickListener() {
            @Override
            public void onClick(ShopCarModel model, int num) {
                adapter.goodsFragment.updateMessage(model, num);
            }
        });
        rvShopCarWindowList.setAdapter(shopCarAdapter);
    }

    @Override
    protected void initTitle() {
        showDefaultBackIcon();
        setTvCenterTitle(shopBean.getShopname());
    }


    @Override
    protected void main() {
        updateShopCar();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    public void updateShopCar() {
        Map<String, String> shopCarInfo = ShopCarUtils.getShopCarInfo(shopBean.getShopid(), login.getUserId());
        tvShopCartAllCount.setText("¥" + shopCarInfo.get("price"));
        if ("0".equals(shopCarInfo.get("num"))) {
            tvCount.setVisibility(View.GONE);
        }else {
            tvCount.setVisibility(View.VISIBLE);
            tvCount.setText(shopCarInfo.get("num"));
        }
    }

    /**
     * 结算
     */
    private void checkout() {
        if (ShopCarUtils.getShopCarData(shopBean.getShopid(), login.getUserId()).isEmpty()) {
            Toast("您当前的购物车为空哦~");
            return;
        }
        //跳转在线订座页
        startActivityForResult(new Intent(context, OnlineReservationsActivity.class)
        .putExtra("shop_info", shopBean), 1000);
    }

    @OnClick({R.id.ll_shop_car_list, R.id.rl_shop_car_icon, R.id.tv_shop_car_window_clear,
    R.id.tv_shop_cart_checkout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_shop_cart_checkout:
                checkout();
                break;
            case R.id.tv_shop_car_window_clear:
                shopCarAdapter.setData(null);
                adapter.goodsFragment.clearAll();
                ShopCarUtils.clearShopCarData(shopBean.getShopid(), login.getUserId());
                updateShopCar();
                break;
            case R.id.ll_shop_car_list:
            case R.id.rl_shop_car_icon:
                if (llShopCarList.getVisibility() == View.VISIBLE) {
                    llShopCarList.setVisibility(View.GONE);
                } else {
                    if (login != null) {
                        shopCarAdapter.setData(ShopCarUtils.getShopCarData(shopBean.getShopid(), login.getUserId()));
                        llShopCarList.setVisibility(View.VISIBLE);
                    } else {
                        startActivity(new Intent(context, LoginActivity.class));
                    }
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1000 && resultCode == 1001) {
            //下单成功，清空购物车
            shopCarAdapter.setData(null);
            adapter.goodsFragment.clearAll();
            ShopCarUtils.clearShopCarData(shopBean.getShopid(), login.getUserId());
            updateShopCar();
        }
    }
}
